
const https = require('https');//node自带请求模块
const cheerio = require('cheerio');//解析html
const fs = require('fs');
https.get('https://movie.douban.com/top250', (res) => {
    let html = '';
    res.on('data', (chunk) => {//拿到响应体了吗？
        html += chunk;

    })
    res.on('end', () => {//当数据彻底拿完
        console.log(html);
        const $ = cheerio.load(html);
        const allData = []
        $('.grid_view .item').each(function () {
            const title = $('.title', this).first().text()
            const star = $('.rating_num', this).text()
            const pic = $('.pic a img', this).attr('src')
            allData.push({
                title,
                star,
                pic
            })



        })
        console.log(allData);
        //保存这份数据
        fs.writeFile('./file.json', JSON.stringify(allData), () => {
            console.log('保存成功');

        })
    })

})
